﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public partial class AxleManager
    {
        /// <summary>
        /// 细分设定值，在进入“控制界面”连接成功 或在重新设定细分完成后，给DSP发一次。
        /*
        格式为：^SX V(VC);>
        ^SY V(VC);>
        ^SZ V(VC);>
        ^ST V(VC);>
        ^SU V(VC);>
        ^SW V(VC);>

        ^ 为通配符；
        S 为步距角细分缩写（Stepper angle fraction）
        ︼ 为空格
        V 为细分值
        （VC）括号里的VC为校验码
        ；> 为结束符
        */
        /*
         DSP 响应格式为（正确）：
            =^SX T(VC);>
            =^SY T(VC);>
            =^SZ T(VC);>
            = ^ST T(VC);>
            =^SU T(VC);>
            =^SW T(VC);>
            错误
            =CMD RE(VC);>

            V 为细分值（1，2，4，8，16，32，64，128）
         */
        /// </summary>
        /// <param name="axleName"></param>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool StepperAngleFraction(string axleName, int sValue)
        {
            if (!IsOpen)
            {
                return false;
            }
            //bool rst = false;
            string data = Protocol.Packet(
                Protocol.GetStepperAngleFraction(axleName,sValue)
                );
            if (AxleCacheManager.AxleList.ContainsKey(axleName))
            {
                AxleCacheManager.AxleList[axleName].AxleDetailSetting = sValue;
            }
            return sendData(data);
        }

        /// <summary>
        /// </summary>
        /// <param name="axleName"></param>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static bool StepperAngleFractionIM(string axleName, int sValue)
        {
            if (!IsOpen)
            {
                return false;
            }
            //bool rst = false;
            string data = Protocol.Packet(
                Protocol.GetStepperAngleFraction(axleName, sValue)
                );
            if (AxleCacheManager.AxleList.ContainsKey(axleName))
            {
                AxleCacheManager.AxleList[axleName].AxleDetailSetting = sValue;
            }

            bool rst = sendData(data);
            if (rst)
            {
                lock (s_spLocker)
                {
                    if (spLink.CanRead)
                    {
                        string msg = spLink.ReadString(2);
                        if (string.IsNullOrEmpty(msg))
                        {
                            return false;
                        }
                        ProtocolV2.UnpacketResult ur = ProtocolV2.Unpack(msg);
                        rst = ur.Result && ur.Action == AxleActions.StepperAngleFractionRespond;
                        if (!rst)
                        {
                            return rst;
                        }
                        OnStepperAngleFractionRespond(ur);
                        return true;
                    }
                }// link lock
            }
            return rst;
        }

        static void OnStepperAngleFractionRespond(ProtocolV2.UnpacketResult ur)
        {
            if (ur.Result && ur.Action == AxleActions.StepperAngleFractionRespond)
            {
                setState(string.Format("[{0}]轴 细分参数设置成功", ur.AxleName));
            }
            AxleCacheManager.Update(ur.AxleName);
        }
    }
}
